package viewset.com.kkcamera.view.image.opengl.filter.graph

import android.content.Context
import android.opengl.GLES20
import com.chan.mediacamera.camera.filter.BaseFilter
import com.chan.mediacamera.util.MatrixTools
import com.chan.mediacamera.util.OpenGlUtils
import java.nio.ByteBuffer
import java.nio.ByteOrder

/**
 * 圆锥fillter
 */
class ConeFillter(var context: Context) : BaseFilter(context) {

    private var mVertexCount = 4
    private var matrixTools: MatrixTools? = null
    internal var r = 1f
    internal var height = 3f

    override fun onCreateShare() {
        mContext?.let {
            mVertexShader = OpenGlUtils.loadShareFromAssetsFile("graph/cone_shader_vertex.glsl", it.resources)
            mFragmentShader = OpenGlUtils.loadShareFromAssetsFile("graph/cone_shader_fragment.glsl", it.resources)
        }
        matrixTools = MatrixTools()
    }

    override fun initBuffer() {
        val dataPos: FloatArray = createConePos()

        // 字节数
        var buffer: ByteBuffer = ByteBuffer.allocateDirect(dataPos.size * 4)
        buffer.order(ByteOrder.nativeOrder())
        mVerBuffer = buffer.asFloatBuffer()
        mVerBuffer.put(dataPos)
        mVerBuffer.position(0)

        //多少个点
        mVertexCount = dataPos.size / 3
    }

    private fun createConePos(): FloatArray {
        // 底部圆360*3 + 顶点1*3 + 底部圆中点1*3
        val points = FloatArray(360 * 3 + 3 + 3)
        // 底部圆心点
        points[0] = 0f
        points[1] = 0f
        points[2] = 0f
        // 顶点
        points[3 * 1] = 0f
        points[3 * 1 + 1] = 0f
        points[3 * 1 + 2] = height
        // 底部圆
        for (i in 0..359) {
            val x = (r * Math.cos(i * (Math.PI / 180f))).toFloat()
            val y = (r * Math.sin(i * (Math.PI / 180f))).toFloat()
            val z = 0f
            points[3 * (i + 2)] = x
            points[3 * (i + 2) + 1] = y
            points[3 * (i + 2) + 2] = z
        }
        return points
    }

    override fun onSizeChanged(width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)

        GLES20.glEnable(GLES20.GL_DEPTH_TEST)

        //计算宽高比
        val ratio = width.toFloat() / height

        //设置透视投影
        //Matrix.frustumM(mProjectMatrix, 0, -ratio, ratio, -1f, 1f, 3f, 17f)
        //设置相机位置
        //Matrix.setLookAtM(mViewMatrix, 0, 0f, -13.0f, -4.0f, 0f, 0f, 0f, 0f, 1.0f, 0.0f)
        //计算变换矩阵
        //Matrix.multiplyMM(mMVPMatrix, 0, mProjectMatrix, 0, mViewMatrix, 0)

        matrixTools?.run {
            //设置透视投影
            frustum(-ratio, ratio, -1f, 1f, 3f, 17f)
            //设置相机位置
            setCamera(1.0f, -10.0f, -4.0f, 0f, 0f, 0f, 0f, 1.0f, 0.0f)
            //计算变换矩阵
            matrix = finalMatrix
        }
    }

    override fun onDraw() {
        GLES20.glEnableVertexAttribArray(glPosition)
        GLES20.glVertexAttribPointer(glPosition, 2, GLES20.GL_FLOAT, false, 0, mVerBuffer)

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mVertexCount)
        GLES20.glDisableVertexAttribArray(glPosition)
    }
}